package com.zdy.state;

/**
 * Created by 逸足天涯
 * on 3/9/2020.
 */
//测试Stop
    //建议线程正常停止，利用次数，不建议死循环
    //不要使用stop()或者destory()等过时方法
public class TestStop implements Runnable{
    //通过外部标志位
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        //线程体使用该标识
        while(flag){
            System.out.println("run.....thread" + i++  );
        }
    }
    public void stop(){
        this.flag = false;
    }
    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main"+ i);
            if (i == 900){
                testStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}
